var ds_type;
var ds_id;
var user;
var ok_btn;
var cancel_btn;
var lang;
var ugid;
var idStr;
var TEMP_DATASET = "temp";

//var objOriginalNum;//for Original object prop
$(document)
		.ready(
				function() {
					$(".operations").hide();
					lang = $("#display-lang").val();
					ds_type = $("#ds_type").val();
					idStr = $("#idStr").val();
					ds_id = $("#ds_id").val();
					user = $("#user").val();
					ugid = $("#ugid").val();
					if(lang == 'en'){
						ok_btn = "OK";
						cancel_btn = "Cancel";
					}else if(lang == 'zh'){
						ok_btn = "确定";
						cancel_btn = "取消";
					}
					
					var dtitle;
					if(lang == 'en'){
						dtitle = "On Loading";
					}else if(lang == 'zh'){
						dtitle = "正在加载";
					}
					$("#onloading").dialog({
						bgiframe : true,
						autoOpen : false,
						draggable : false,
						closeOnEscape : false,
						closeText : false,
						resizable : false,
						width : 400,
						title : dtitle,
						modal : true
					});
					$("#onloading").parent().appendTo("#dialog_target");
					$('a.ui-dialog-titlebar-close').hide();

					/*$("#relations").accordion({
						collapsible : true,
						autoHeight : false
					});*/
					//=========for Original object prop
					var data = {};
					data["ds_type"] = ds_type;
					data["idStr"] = idStr;
					data["ds_id"] = ds_id+"";
					data["lang"] = lang;//=====
					data = JSON.stringify(data);
					data = ("objectOriginalNum=" + encodeURIComponent(data));
//					data = ("originalNum=" + encodeURIComponent(data));
					$.ajax({
								url : "/sview/relationOriginal",
								async : false,//true,
								data : data,
								dataType : "json",
								type : "post",
								success : function(result) {
									var objOriginalNum = result["totalNum"];
									$("#tab-relation").attr("objOriginalNum",objOriginalNum);
								}
							});
					//=================================
//					var new_box = $(".nv-box");
//					$(new_box).append("<a id='new_button' class='rcreate' href='javascript:void(0)' title='Create a new relation'>New</a>");
					if (user == "null" || ugid==undefined || ds_type==TEMP_DATASET) {
						$("#edit_button").hide();
						$("#new_button").hide();
						$(".tab.ordinary").addClass("selected");
						showOrdinary(0, 10, true, ds_type, ds_id,idStr, user, lang,1);
					} else {
//						$("#new_button").hide();
						$(".tab.favorite").addClass("selected");
						showFavorite(0, 10, true, ds_type, ds_id, user, lang,1);
//						if(num<=0){
//							$(".tab.favorite").removeClass("selected");
//							$(".tab.ordinary").addClass("selected");
//							var onum = showOrdinary(0, 10, true, ds_type, ds_id, user, lang,1);
//							if(onum<=0){
//								$(".tab.ordinary").removeClass("selected");
//								$(".tab.original").addClass("selected");
//								showOriginal(0, 10, true, ds_type, ds_id, user, lang, 1);
//							}
//						}
					}

					/*$("#favorite").click(function(event) {
						showFavorite(0, 10, true, ds_type, ds_id, user, lang);
					});
					$("#ordinary").click(function(event) {
						showOrdinary(0, 10, true, ds_type, ds_id, user, lang);
					});
					$("#trash").click(function(event) {
						showTrash(0, 10, true, ds_type, ds_id, user, lang);
					});*/
					$(".tab").click(function(){
						var currentPage = 1;
						if($(this).hasClass("selected")){
							 return;
						 }
						var oldSelected =  $(".tab.selected");
						oldSelected.removeClass("selected");
						$(this).addClass("selected");
						
//						storeOldViewCache(oldSelected);
//						
//						var flag = readViewCache();
//						if(flag){
//							 return;
//						}
						if($(this).hasClass("favorite")){
							showFavorite(0, 10, true, ds_type, ds_id, user, lang,currentPage);
						}else if($(this).hasClass("ordinary")){
							showOrdinary(0, 10, true, ds_type, ds_id, idStr, user, lang ,currentPage);
						}else if($(this).hasClass("trash")){
							showTrash(0, 10, true, ds_type, ds_id, user, lang,currentPage);
						}else if($(this).hasClass("original")){
							showOriginal(0, 10, true, ds_type, ds_id, idStr, user, lang,currentPage);
						}
					});
					
					
					/*$("#favorite").click(function(event) {
						showFavorite(0, 10, true, ds_type, ds_id, user, lang);
					});
					$("#ordinary").click(function(event) {
						showOrdinary(0, 10, true, ds_type, ds_id, user, lang);
					});
					$("#trash").click(function(event) {
						showTrash(0, 10, true, ds_type, ds_id, user, lang);
					});*/

					/* cancel favorite */
					$("a.down").live("click", function(event) {
						var line = $(this).parent();
						cancelFavorite(line, ds_type, ds_id, user);
					});

					/* Favourite and update the database. favor and cancel favor */
					$("a.top").live("click", function() {
						var line = $(this).parent();
						addFavorite(line, ds_type, ds_id, user);
					});

					/*
					 * Remove into trash and update the database. trash and
					 * cancel trash
					 */
					$("a.remove").live("click", function() {
						var line = $(this).parent();
						addTrash(line, ds_type, ds_id, user);
					});

					$("a.restore").live("click", function() {
						var line = $(this).parent();
						cancelTrash(line, ds_type, ds_id, user);
					});

//					$("span.relation").live("click", function() {
					$(".uRelation").live("click", function() {
						if($(this).hasClass("rnav-select")){//("showing")){
							return;
						}else{
//							if($(this).hasClass("originalRelation")){
//								
//							}else{
								var id = $(this).attr("rid");
								useRelation($(this),id,ds_type, ds_id,idStr, false);
//							}
						}
//						getRelationResult(id, ds_type, ds_id);
//						getRelationMeta(id);
					});
					$(".rset" ).live("click",function(event){
				        $('#smenu').toggle();
				        event.stopPropagation();
					});
					$(".relation-list li").live("mouseover",function(){
						$(this).addClass("bh");
						$(this).find(".rel_op").css("display","inline");
					})
					$(".relation-list li").live("mouseout",function(){
						$(this).removeClass("bh");
						$(this).find(".rel_op").css("display","none");
					})
					$(".rel-edit")
							.live(
									"click",
									function() {
										var url = location.href;
						    			var paraString = encodeURIComponent(url);
										var relation_id = $(this).attr(
												"relation_id");
//										var href = "/sview/relations/relation_create_edit.jsp?purpose=edit&relation_id="
//												+ relation_id
//												+ "&ds_type="
//												+ ds_type + "&ds_id=" + ds_id+"&lang="+lang ;
										var href = "/sview/relations/relation_edit.jsp?relation_id="
											+ relation_id
											+ "&ds_type=" + ds_type + "&ds_id=" + ds_id+"&idStr="+idStr+"&lang="+lang+"&from="+paraString;
//										window.open(encodeURI(href));//open new window
										window.location = encodeURI(href);//open in current window
									});
					$(".rel-translate")
					.live(
							"click",
							function() {
								var relation_id = $(this).attr(
										"relation_id");
								var gid = $(this).attr("gid");
								var url = location.href;
				    			var paraString = encodeURIComponent(url);
								var href = "/sview/relations/relation_translate.jsp?" +
										"relation_id="+ relation_id
										+ "&ds_type="+ ds_type 
										+ "&ds_id=" + ds_id
										+"&idStr="+idStr
										+ "&gid=" + gid
										+"&lang="+lang 
										+"&from="+paraString;
//								window.open(encodeURI(href));//open new window
								window.location = encodeURI(href);//open in current window
							});
					$("#new_button")
							.live(
									"click",
//									function() {
//										var href = "/relations/relation_create_edit.jsp?purpose=new&ds_type="
//												+ ds_type + "&ds_id=" + ds_id;
//										window.open($("#basePath").val()
//												+ encodeURI(href));
//									});
									function(){
										var url = location.href;
						    			var paraString = encodeURIComponent(url);
										if($("#ugid").length>0){
//											var href = "/sview/relations/relation_create_edit.jsp?purpose=new&ds_type="
//												+ ds_type + "&ds_id=" + ds_id + "&lang="+lang;//"&gid="+gid+
											var href = "/sview/relations/relation_create_start.jsp?purpose=new&ds_type="
												+ ds_type + "&ds_id=" + ds_id+"&idStr="+idStr + "&lang="+lang+"&from="+paraString;
//											window.open(encodeURI(href));
//											window.location.reload();
											window.location = encodeURI(href);//open in current window
										}else{
//											var url = location.href;
											   var edit_authorDiv = createGroupSelectionDiv(getUser(),lang);
											   $(edit_authorDiv).dialog({
										    		 modal:true,
													 minWidth:450,
											         height:200,
											         buttons:[{
															text: ok_btn,
															click :function(){
											        		var type = $(".edit_author").attr("t") ;
											    			var paraString = encodeURIComponent(url);
											        		if(type=="y"){
											        			var gid = $(".edit_author select>option:selected").attr("gid");
												    			var datasetid = parseInt($(".entity-label").attr("datasetId"));
//												    			var location = "relations/relation_create_edit.jsp?ds_type="+ds_type+"&datasetid="+datasetid+"&gid="+gid+"&lang="+displayLang;
//												    			window.location= location;
//												    			var href = "/sview/relations/relation_create_edit.jsp?purpose=new&ds_type="
//																	+ ds_type + "&ds_id=" + ds_id + "&lang="+lang;//"&gid="+gid+
												    			var href = "/sview/relations/relation_create_start.jsp?purpose=new&ds_type="
																	+ ds_type + "&ds_id=" + ds_id+"&idStr="+idStr + "&lang="+lang+"&from="+paraString;//"&gid="+gid+
												    			setGroup();//===========
//																window.open(encodeURI(href));
//																window.location.reload();
																window.location = encodeURI(href);//open in current window
												    			 
											        		}else if(type=="n"){
//												    			var paraString = encodeURIComponent(url);
												    			var location = "/sview/group/gmanage.jsp?from="+paraString;
												    			if(lang=="zh")
												    				location = "/sview/group/gmanage.jsp?lang=zh&from="+paraString;
												    			window.location= location;
											        		}
															 $(".edit_author").dialog("close");
													     }
											         },{
																text: cancel_btn,
																click :function(){
														 $(".edit_author").dialog("close");
													    }
											         }]
										       });
										}//end of if_else
									   
								   });
					
				});
//isTop=true if the use of relation is because it is the first relation in the list
//in this case, not add the use_time
function useRelation(li,id,ds_type, ds_id,idStr, isTop){
	if($(li).length<=0){
		return;
	}
//	var old = $(".showing");
	var old = $(".rnav-select");
	if($(old).length>0){
		$(old).removeClass("rnav-select");//("showing");
		$(old).find(".nav-arrow").remove();
	}
	$(li).addClass("rnav-select");//("showing");
	var span = $("<span class='nav-arrow'></span>");
    $(li).append(span);
    if($(li).hasClass("originalRelation")){
    	getOriginalResult($(li),ds_type, ds_id,idStr);
    	getOriginalMeta($(li));
    }else{
    	getRelationResult(id, ds_type, ds_id,idStr, isTop);
    	getRelationMeta(id);
    }
}
function getOriginalMeta(li) {
	var name = $(li).attr("name");
	$("#rel_title").text(name);
	$("#rel_meta").html("");
	/*if(canedit == "true"){
		$(".operations").show();
		$(".rel-edit").attr("relation_id", id);
		$(".rel-translate").attr("relation_id", id);
		$(".rel-translate").attr("gid",gid);
	}else{
		$(".operations").hide();
	}	*/	
}
function getRelationMeta(id) {
	var data = {};
	data["id"] = id+"";
	data["user"] = $("#user").val();
	data = JSON.stringify(data);
	data = ("data=" + encodeURIComponent(data));
	$.ajax({
		url : "/sview/relationInfo",
		async : true,
		data : data,
		dataType : "json",
		type : "post",
		success : function(result) {
			var name = result["name"];
//			var user = result["user"];
			var group = result["group"];
			var date = result["date"];
			var canedit = result["canedit"];
			var gid=result["gid"];
//			alert(group+canedit);
			$("#rel_title").text(name);
//			var meta = $("<div id='rel_meta'></div>");
			
			if(lang == 'en'){
				$("#rel_meta").html("group: " + group + "<br />" + "Date: " + date);
//				$(meta).html("group: " + group + "<br />" + "Date: " + date);
			}else if(lang == 'zh'){
				$("#rel_meta").html("编辑组: " + group + "<br />" + "创建日期: " + date);
//				$(meta).html("编辑组: " + group + "<br />" + "创建日期: " + date);
			}
			if(canedit == "true"){
				$(".operations").show();
				$(".rel-edit").attr("relation_id", id);
				$(".rel-translate").attr("relation_id", id);
				$(".rel-translate").attr("gid",gid);
//				if(lang == 'en'){
//					$("#edit_button").text("Edit");
//				}else if (lang == 'zh'){
//					$("#edit_button").text("编辑");
//				}
//				$("#edit_button").show();
			}else{
//				$("#edit_button").hide();
				$(".operations").hide();
			}
		}
	});
}
function getOriginalResult(li, ds_type, ds_id,idStr) {
	$("#rel_table").addClass("loading2");
	var data = {};
	data["roles"] = $(li).attr("roles");
	data["uri"] = $(li).attr("uri");
	data["type"] = $(li).attr("type");
	data["ds_type"] = ds_type;
	data["ds_id"] = ds_id+"";
	data["idStr"] = idStr;
	data = JSON.stringify(data);
	data = ("getResult=" + encodeURIComponent(data));
	
//	$("#onloading").dialog("open");
	$.ajax({
		url : "/sview/relationOriginal",
		async : true,
		data : data,
		dataType : "json",
		type : "post",
		success : function(result) {
			$("#rel_table").removeClass("loading2");
			var lang = $("#display-lang").val();
//			$("#onloading").dialog("close");
			var table = $("<table></table>");
			$("#rel_table").html(table);
			var array = result["result"];
			var tableTitle = array[0];
			var trhead = $("<tr></tr>");
			table.append(trhead);
			for ( var i = 0; i < tableTitle.length; i++) {
				var column = tableTitle[i];
				var v = column["v"];
				var vname = column["vname"];
				var th = $("<th></th>");
				th.attr("v", v);
				th.text(vname);
				trhead.append(th);
			}
			for ( var i = 1; i < array.length; i++) {
				var trbody = $("<tr></tr>");
				table.append(trbody);
				var item = array[i];
				for ( var j = 0; j < item.length; j++) {
					var ele = item[j];
					var v = ele["v"];
					var name = new String(ele["name"]);
					var content = ele["content"];
					var uri_id = ele["uri_id"];
					var dis = ele["displayName"].trim();//content;
					if(dis.length==0){
						dis = content;
					}
					var td = $("<td></td>");
					trbody.append(td);
					td.attr("v", v);
					td.attr("name", name);
					var div = $("<span></span>");
					if(uri_id != null){
						var a = $("<a></a>");
						var href = "/sview/entity/o"+uri_id;
						if(lang=="zh"){
							href += "?lang=zh";
						}
						$(a).attr("href",href);
						$(a).attr("title", content);
						$(a).text(dis);
						div.append(a);
					}else{
						div.attr("title", content);
						div.text(dis);
					}
					td.append(div);
				}
			}
			
		}
	});
}
function getRelationResult(id, ds_type, ds_id,idStr, isTop) {
	$("#rel_table").addClass("loading2");
	var data = {};
//	alert("gettingResult:"+id);
	data["id"] = id+"";
	data["ds_type"] = ds_type;
	data["ds_id"] = ds_id+"";
	data["idStr"] = idStr;
	data["new"] = "new";
	data["isTop"] = isTop;
	data = JSON.stringify(data);
	data = ("data=" + encodeURIComponent(data));
//	$("#onloading").dialog("open");
	$.ajax({
		url : "/sview/relationResult",
		async : true,
		data : data,
		dataType : "json",
		type : "post",
		success : function(result) {
			$("#rel_table").removeClass("loading2");
			lang = $("#display-lang").val();
//			$("#onloading").dialog("close");
//			var meta = $("#rel_meta").html();
			var table = $("<table></table>");
			$("#rel_table").html(table);
//			var table = $("#rel_table table");
			var array = result["result"];
			var tableTitle = array[0];
			var trhead = $("<tr></tr>");
			table.append(trhead);
			for ( var i = 0; i < tableTitle.length; i++) {
				var column = tableTitle[i];
				var v = column["v"];
				var vname = column["vname"];
				var th = $("<th></th>");
				th.attr("v", v);
				th.text(vname);
				trhead.append(th);
			}
			for ( var i = 1; i < array.length; i++) {
				var trbody = $("<tr></tr>");
				table.append(trbody);
				var item = array[i];
				for ( var j = 0; j < item.length; j++) {
					var ele = item[j];
					var v = ele["v"];
					var name = new String(ele["name"]);
					var content = ele["content"];
					var uri_id = ele["uri_id"];
					var dis = ele["displayName"].trim();//content;
					if(dis.length==0){
						dis = content;
					}
//					if(content=="http://www.advogato.org/person/sye/foaf.rdf#me"){
//						alert("|"+dis+"|"+dis.length+"|"+dis.trim().length);
//					}
//					if (name.indexOf("u") == 0) {
//						dis = getLocalname(content);
//					}
					var td = $("<td></td>");
					trbody.append(td);
					td.attr("v", v);
					td.attr("name", name);
					var div = $("<span></span>");
					if(uri_id != null){
						var a = $("<a></a>");
						var href = "/sview/entity/o"+uri_id;
						if(lang=="zh"){
							href += "?lang=zh";
						}
						$(a).attr("href",href);
						$(a).attr("title", content);
						$(a).text(dis);
						div.append(a);
					}else{
						div.attr("title", content);
						div.text(dis);
					}
					td.append(div);
				}
			}
		}
	});
}
function getOriginal(start, length, newFlag, ds_type, ds_id,idStr, user, lang, currentPage) {
	var data = {};
	data["start"] = start;
	data["length"] = length;
	data["ds_type"] = ds_type;
	data["ds_id"] = ds_id+"";
	data["idStr"] = idStr;
	data["user"] = user;
	data["lang"] = lang;//=====
	data = JSON.stringify(data);
	data = ("objectOriginal=" + encodeURIComponent(data));
	$.ajax({
				url : "/sview/relationOriginal",
				async : false,//true,
				data : data,
				dataType : "json",
				type : "post",
				success : function(result) {
					var array = result["result"];
					var totalNum = result["totalNum"];
					var totalPage = Math.ceil(totalNum/length);
					var flag = false;
					if (newFlag) {
//						$(".favorite").html("");
						$(".vnav").html("");
					}
//					if ($(".favorite ol").length > 0)
					if($(".vnav ul").length > 0)
						flag = true;
					if (!flag) {
//						var olt = $("<ol></ol>");
//						$(".favorite").append(olt);
						var ult = $("<ul></ul>");
						ult.addClass("relation-list");
						$(".vnav").append(ult);
					}
//					var ol = $(".favorite>ol");
					var ul = $(".vnav>ul");
					for ( var i = 0; i < array.length; i++) {
						var element = array[i];
						var id = element["id"];
						var type = element["type"];
						var name = element["name"];
						var roles = element["roles"];
						var uri = element["uri"];
						var li = $("<li></li>");
						li.addClass("uRelation rnav-select originalRelation");
						li.attr("rid",id);
						li.attr("roles",roles);
						li.attr("uri",uri);
						li.attr("type",type);
						li.attr("name",name);
						ul.append(li);
//						var div = $('<span class="split"></span>');
//						li.append(div);
						var row = $("<span></span>");
						row.attr("id", id);
						row.attr("from", "original");
						li.append(row);
//						var relation = $("<a class='relation' href='javascript:void(0)'></a>");
						var relation = $("<span class='relation'></span>");
						row.append(relation);
						relation.attr("id", id);
						relation.attr("from", "original");
						relation.attr("title",uri);
						relation.text(name + "(" + roles + ")");
					}
					if(newFlag){//if(array.length > 0){
						useRelation($(".relation-list li:first"),-1, ds_type, ds_id, idStr, true);
					}
					$(".vnav").attr("num",array.length);
				}
			});
	if($("#new_button").length>0){
		$("#new_button").hide();
	}
	return $(".vnav").attr("num");
	
}
function showOriginal(start, length, newFlag, ds_type, ds_id,idStr, user, lang,currentPage) {
	var data = {};
	data["start"] = start;
	data["length"] = length;
	data["ds_type"] = ds_type;
	data["ds_id"] = ds_id+"";
	data["idStr"] = idStr;
	data["user"] = user;
	data["lang"] = lang;//=====
	data = JSON.stringify(data);
	data = ("original=" + encodeURIComponent(data));
//	data = ("objectOriginal=" + encodeURIComponent(data));
	$.ajax({
				url : "/sview/relationOriginal",
				async : false,//true,
				data : data,
				dataType : "json",
				type : "post",
				success : function(result) {
					var array = result["result"];
					var totalNum = result["totalNum"];
					var totalPage = Math.ceil(totalNum/length);
					var flag = false;
					if (newFlag) {
//						$(".favorite").html("");
						$(".vnav").html("");
					}
//					if ($(".favorite ol").length > 0)
					if($(".vnav ul").length > 0)
						flag = true;
					if (!flag) {
//						var olt = $("<ol></ol>");
//						$(".favorite").append(olt);
						var ult = $("<ul></ul>");
						ult.addClass("relation-list");
						$(".vnav").append(ult);
					}
//					var ol = $(".favorite>ol");
					var ul = $(".vnav>ul");
					for ( var i = 0; i < array.length; i++) {
						var element = array[i];
						var id = element["id"];
						var type = element["type"];
						var name = element["name"];
						var roles = element["roles"];
						var uri = element["uri"];
						var li = $("<li></li>");
						li.addClass("uRelation rnav-select originalRelation");
						li.attr("rid",id);
						li.attr("roles",roles);
						li.attr("uri",uri);
						li.attr("type",type);
						li.attr("name",name);
						ul.append(li);
//						var div = $('<span class="split"></span>');
//						li.append(div);
						var row = $("<span></span>");
						row.attr("id", id);
						row.attr("from", "original");
						li.append(row);
//						var relation = $("<a class='relation' href='javascript:void(0)'></a>");
						var relation = $("<span class='relation'></span>");
						row.append(relation);
						relation.attr("id", id);
						relation.attr("from", "original");
						relation.attr("title",uri);
						relation.text(name + "(" + roles + ")");
					}
					//paging information
					var container = $(".vnav");
					var navDiv = $("<div class='navfoot'></div>");
					container.append(navDiv);
					if(currentPage>1){
						var prevaLink = $('<a  class="navContent pnnav" href="javascript:void(0)"></a>');
						prevaLink.bind("click",function(){
							var mstart = (start-length)<0?0:(start-length);
//							getViews(type,isInner, minClassID,user,mstart,length,lang,currentPage-1);
							showOriginal(mstart, length, newFlag, ds_type, ds_id,idStr, user, lang,currentPage-1);
						});
						if(lang=='en')
							prevaLink.text("Prev");
						else if(lang=="zh")
							prevaLink.text("上一页");
						navDiv.append(prevaLink);
					}
					/*in the view paging, 5 pages shown*/
					var startPage = (currentPage-2)>0?(currentPage-2):1;
					var endPage = (currentPage+2)>totalPage?(totalPage>0?totalPage:1):(currentPage+2);
					if(endPage>startPage){
					for(var j=startPage;j<=endPage;j++){
						if(j!=currentPage){
							var alink =$('<a class="navContent" href="javascript:void(0)"></a>');
							alink.text(j);
							alink.click(function(){
								var pointer = parseInt($(this).text());
								showOriginal((pointer-1)*length, length, newFlag, ds_type, ds_id,idStr, user, lang,pointer);
							});
							navDiv.append(alink);
						}else{
							var span = $("<span class='navContent current' ></span>");
							span.text(j);
							navDiv.append(span);
						}
					}
					}
					if(currentPage<totalPage){
						var nextaLink = $('<a  class="navContent pnnav" href="javascript:void(0)"></a>');
						nextaLink.bind("click",function(){
//							getViews(type, isInner,minClassID,user,start+length,length,lang,currentPage+1);
							showOriginal(start+length, length, newFlag, ds_type, ds_id,idStr, user, lang,currentPage+1);
						});
						if(lang=='en')
							nextaLink.text("Next");
						else if(lang=="zh")
							nextaLink.text("下一页");
						navDiv.append(nextaLink);
					}	
					
					if(array.length > 0){
						useRelation($(".relation-list li:first"),-1, ds_type, ds_id,idStr, true);
					}
					$(".vnav").attr("num",array.length);
					
				}
			});
	if($("#new_button").length>0){
		$("#new_button").hide();
	}
	return $(".vnav").attr("num");
}
function showFavorite(start, length, newFlag, ds_type, ds_id, user, lang,currentPage) {
	$(".vnav").addClass("loading2");
	var data = {};
	data["start"] = start;
	data["length"] = length;
	data["ds_type"] = ds_type;
	data["ds_id"] = ds_id+"";
	data["idStr"] = idStr;
	data["user"] = user;
	data["lang"] = lang;//=====
	data = JSON.stringify(data);
	data = ("favorite=" + encodeURIComponent(data));
	$.ajax({
				url : "/sview/relationFavorite",
				async : false,//true,
				data : data,
				dataType : "json",
				type : "post",
				success : function(result) {
					$(".vnav").removeClass("loading2");
					var array = result["result"];
					var totalNum = result["totalNum"];
					var totalPage = Math.ceil(totalNum/length);
					var flag = false;
					if (newFlag) {
//						$(".favorite").html("");
						$(".vnav").html("");
					}
//					if ($(".favorite ol").length > 0)
					if($(".vnav ul").length > 0)
						flag = true;
					if (!flag) {
//						var olt = $("<ol></ol>");
//						$(".favorite").append(olt);
						var ult = $("<ul></ul>");
						ult.addClass("relation-list");
						$(".vnav").append(ult);
					}
//					var ol = $(".favorite>ol");
					var ul = $(".vnav>ul");
					for ( var i = 0; i < array.length; i++) {
						var element = array[i];
						var id = element["id"];
						var li = $("<li></li>");
						li.addClass("uRelation");
						li.attr("rid",id);
						ul.append(li);
//						var div = $('<span class="split"></span>');
//						li.append(div);
						var name = element["name"];
						var roles = element["roles"];
						var row = $("<span></span>");
						row.attr("id", id);
						row.attr("from", "favorite");
						li.append(row);
//						var relation = $("<a class='relation' href='javascript:void(0)'></a>");
						var relation = $("<span class='relation'></span>");
						row.append(relation);
						relation.attr("id", id);
						relation.attr("from", "favorite");
						relation.text(name + "(" + roles + ")");
						relation.attr("title",name + "(" + roles + ")");
						var down = $("<a class='down rel_op' href='javascript:void(0)'></a>");
						if(lang=='en'){
							down.attr("title","Cancel Favourite");
				    	}else if(lang=='zh'){
				    		down.attr("title","取消最爱");
						}
						down.hide();
						row.append(down);
						var remove = $("<a class='remove rel_op' href='javascript:void(0)'></a>");
						if(lang=='en'){
							remove.attr("title","Remove");
				    	}else if(lang=='zh'){
				    		remove.attr("title","移除");
				    	}
						remove.hide();
						row.append(remove);
					}
					//paging information
					var container = $(".vnav");
					var navDiv = $("<div class='navfoot'></div>");
					container.append(navDiv);
					if(currentPage>1){
						var prevaLink = $('<a  class="navContent pnnav" href="javascript:void(0)"></a>');
						prevaLink.bind("click",function(){
							var mstart = (start-length)<0?0:(start-length);
							showFavorite(mstart, length, newFlag, ds_type, ds_id, user, lang,currentPage-1);
						});
						if(lang=='en')
							prevaLink.text("Prev");
						else if(lang=="zh")
							prevaLink.text("上一页");
						navDiv.append(prevaLink);
					}
					/*in the view paging, 5 pages shown*/
					var startPage = (currentPage-2)>0?(currentPage-2):1;
					var endPage = (currentPage+2)>totalPage?(totalPage>0?totalPage:1):(currentPage+2);
					if(endPage>startPage){
					for(var j=startPage;j<=endPage;j++){
						if(j!=currentPage){
							var alink =$('<a class="navContent" href="javascript:void(0)"></a>');
							alink.text(j);
							alink.click(function(){
								var pointer = parseInt($(this).text());
								showFavorite((pointer-1)*length, length, newFlag, ds_type, ds_id, user, lang,pointer);
							});
							navDiv.append(alink);
						}else{
							var span = $("<span class='navContent current' ></span>");
							span.text(j);
							navDiv.append(span);
						}
					}
					}
					if(currentPage<totalPage){
						var nextaLink = $('<a  class="navContent pnnav" href="javascript:void(0)"></a>');
						nextaLink.bind("click",function(){
//							getViews(type, isInner,minClassID,user,start+length,length,lang,currentPage+1);
							showFavorite(start+length, length, newFlag, ds_type, ds_id, user, lang,currentPage+1);
						});
						if(lang=='en')
							nextaLink.text("Next");
						else if(lang=="zh")
							nextaLink.text("下一页");
						navDiv.append(nextaLink);
					}		
					
					//========
					if(array.length > 0){
						useRelation($(".relation-list li:first"),array[0]["id"], ds_type, ds_id,idStr, true);
					}
					$(".vnav").attr("num",array.length);
//					if (array.length > 0) {
//						var div = $("<div class='more-container'></div>");
//						var moreDiv = $("<div class='moreDiv'></div>");
//						var showText;
//						if(lang == 'en'){
//							showText = "Load More";
//						}else if(lang == 'zh'){
//							showText = "更多";
//						}
//						var a = $('<a href="javascript:void(0)"><span class="tip">'+showText+'</span><span class="arrowDown"></span></a>');
//						a.bind("click", function() {
//							showFavorite(start + length, length, false,
//									ds_type, ds_id, user);
//						});
//						moreDiv.append(a);
//						div.append(moreDiv);
//						$(".favorite").append(div);
//					}
				}
			});
	if(user!=null){
		$("#new_button").show();
	}
	return $(".vnav").attr("num");
}
/**
 * 
 * @param start: if(ds_type="temp"||ds_id<0) start= start_relation_id  else start=start_num
 * @param length
 * @param newFlag
 * @param ds_type
 * @param ds_id
 * @param idStr
 * @param user
 * @param lang
 * @param currentPage
 */
function showOrdinary(start, length, newFlag, ds_type, ds_id,idStr, user, lang,currentPage, backward) {
	$(".vnav").addClass("loading2");
	var max_id = 0;
	var data = {};
	data["start"] = start;
	data["length"] = length;
	data["ds_type"] = ds_type;
	data["ds_id"] = ds_id+"";
	data["idStr"] = idStr;
	data["user"] = user;
	data["lang"] = lang;
	if(backward!=null &&　backward==true){
		data["backward"] = backward;
	}
	data = JSON.stringify(data);
	data = ("ordinary=" + encodeURIComponent(data));
	$.ajax({
				url : "/sview/relationOrdinary",
				async : true,
				data : data,
				dataType : "json",
				type : "post",
				success : function(result) {
					$(".vnav").removeClass("loading2");
					var array = result["result"];
					var totalNum = result["totalNum"];
//					var totalPage = Math.ceil(totalNum/length);
					var flag = false;
					if (newFlag==true) {
//						$(".ordinary").html("");
						$(".vnav").html("");
					}
//					if ($(".ordinary ol").length > 0)
					if ($(".vnav ul").length > 0)
						flag = true;
					if (!flag) {
//						var olt = $("<ol></ol>");
//						$(".ordinary").append(olt);
						var ult = $("<ul></ul>");
						ult.addClass("relation-list");
						$(".vnav").append(ult);
					}
//					var ol = $(".ordinary>ol");
					var ul = $(".vnav>ul");
					for ( var i = 0; i < array.length; i++) {
						var element = array[i];
						var id = element["id"];
						var li = $("<li></li>");
						li.addClass("uRelation rnav-select");
						li.attr("rid",id);
						ul.append(li);
//						var div = $('<span class="split"></span>');
//						li.append(div);
						var name = element["name"];
						var roles = element["roles"];
						var row = $("<span></span>");
						row.attr("id", id);
						row.attr("from", "ordinary");
						li.append(row);
//						var relation = $("<a class='relation' href='javascript:void(0)'></a>");
						var relation = $("<span class='relation'></span>");
						relation.attr("id", id);
						relation.attr("from", "ordinary");
						relation.attr("title",name + "(" + roles + ")");
						relation.text(name + "(" + roles + ")");
						row.append(relation);
						if (user != "null" && ds_type != "temp" && ds_id>0) {
							var down = $("<a class='top rel_op' href='javascript:void(0)'></a>");
							if(lang=='en'){
								down.attr("title","Favourite");
					    	}else if(lang=='zh'){
					    		down.attr("title","最爱");
							}
							down.hide();
							row.append(down);
							var remove = $("<a class='remove rel_op' href='javascript:void(0)'></a>");
							if(lang=='en'){
								remove.attr("title","Remove");
					    	}else if(lang=='zh'){
					    		remove.attr("title","移除");
					    	}
							remove.hide();
							row.append(remove);
						}
						max_id = id;
					}
					//=========for Original object prop
					var totalOrdinaryPage = Math.ceil(totalNum/length);
					var objOriginalNum = $("#tab-relation").attr("objOriginalNum");
					var totalPage = Math.ceil((totalNum*1+objOriginalNum*1)/length);
//					alert(objOriginalNum+"|"+totalNum+"|"+totalPage+"|"+(totalNum*1+objOriginalNum*1)+"|"+length);
					if(array.length < length){
						var dis = length - array.length;
						var ostart = (currentPage-1)*length-totalNum;
						ostart = (ostart<0)?0:ostart;
						
						if(dis<length){
							getOriginal(ostart, dis, false, ds_type, ds_id, idStr, user, lang,currentPage+1);
						}else{
							getOriginal(ostart, dis, true, ds_type, ds_id, idStr, user, lang,currentPage+1);
						}
//						alert(currentPage+"|"+totalPage+"|"+objOriginalNum+"|"+totalNum+"|"+newFlag+"|"+array.length+"|"+dis+"|"+ostart);
						
//						alert(array.length);
					}
					//=================
					//paging information
					var container = $(".vnav");
					var navDiv = $("<div class='navfoot'></div>");
					container.append(navDiv);
					if(currentPage>1){
						var prevaLink = $('<a  class="navContent pnnav" href="javascript:void(0)"></a>');
						prevaLink.bind("click",function(){
							if(ds_id<0){
								var maxstart = $("ul.relation-list .uRelation:last").attr("rid");
								showOrdinary(parseInt(maxstart), length, newFlag, ds_type, ds_id, idStr, user, lang,currentPage-1, true);
							}else{
								var mstart = (start-length)<0?0:(start-length);
								showOrdinary(mstart, length, newFlag, ds_type, ds_id, idStr, user, lang,currentPage-1);
							}
						});
						if(lang=='en')
							prevaLink.text("Prev");
						else if(lang=="zh")
							prevaLink.text("上一页");
						navDiv.append(prevaLink);
					}
					/*in the view paging, 5 pages shown*/
					var startPage = (currentPage-2)>0?(currentPage-2):1;
					var endPage = (currentPage+2)>totalPage?(totalPage>0?totalPage:1):(currentPage+2);
					if(endPage>startPage){
					for(var j=startPage;j<=endPage;j++){
						if(j!=currentPage){
							var alink =$('<a class="navContent" href="javascript:void(0)"></a>');
							alink.text(j);
							alink.click(function(){
								var pointer = parseInt($(this).text());
//								alert("pointer:"+pointer+"|"+(pointer-1)*length+"|"+length);
								showOrdinary((pointer-1)*length, length, newFlag, ds_type, ds_id,idStr, user, lang,pointer);
							});
							navDiv.append(alink);
						}else{
							var span = $("<span class='navContent current' ></span>");
							span.text(j);
							navDiv.append(span);
						}
					}
					}
					if(currentPage<totalPage){
						var nextaLink = $('<a  class="navContent pnnav" href="javascript:void(0)"></a>');
						nextaLink.bind("click",function(){
							showOrdinary(start+length, length, newFlag, ds_type, ds_id,idStr, user, lang,currentPage+1);
						});
						if(lang=='en')
							nextaLink.text("Next");
						else if(lang=="zh")
							nextaLink.text("下一页");
						navDiv.append(nextaLink);
					}		
					
					//========
					
					if(array.length > 0){
						useRelation($(".relation-list li:first"),array[0]["id"], ds_type, ds_id,idStr, true);
					}
					$(".vnav").attr("num",array.length);
//					if (array.length > 0) {
//						var div = $("<div class='more-container'></div>");
//						var moreDiv = $("<div class='moreDiv'></div>");
//						var showText;
//						if(lang == 'en'){
//							showText = "Load More";
//						}else if(lang == 'zh'){
//							showText = "更多";
//						}
//						var a = $('<a href="javascript:void(0)"><span class="tip">'+showText+'</span><span class="arrowDown"></span></a>');
//						a.bind("click", function() {
//							showOrdinary(max_id + 1, length, false, ds_type,
//									ds_id, user);
//						});
//						moreDiv.append(a);
//						div.append(moreDiv);
//						$(".ordinary").append(div);
//					}
				}
			});
//	alert(user+"|"+(ds_type=="temp" && user!="null")+"|"+$("#new_button").length);
	if(ds_type=="temp" && user!=null && user!="null"){
		$("#new_button").show();
	}else if($("#new_button").length>0){
		$("#new_button").hide();
	}
}

function showTrash(start, length, newFlag, ds_type, ds_id, user, lang,currentPage) {
	$(".vnav").addClass("loading2");
	var num = -1;
	var data = {};
	data["start"] = start;
	data["length"] = length;
	data["ds_type"] = ds_type;
	data["ds_id"] = ds_id+"";
	data["user"] = user;
	data["lang"] = lang;
	data = JSON.stringify(data);
	data = ("trash=" + encodeURIComponent(data));
	$
			.ajax({
				url : "/sview/relationTrash",
				async : true,
				data : data,
				dataType : "json",
				type : "post",
				success : function(result) {
					$(".vnav").removeClass("loading2");
					var array = result["result"];
					var totalNum = result["totalNum"];
					var totalPage = Math.ceil(totalNum/length);
//					alert(totalNum+"|"+totalPage);
					var flag = false;
					if (newFlag) {
//						$(".trash").html("");
						$(".vnav").html("");
					}
//					if ($(".trash ol").length > 0)
					if($(".vnav ol").length > 0)
						flag = true;
					if (!flag) {
//						var olt = $("<ol></ol>");
//						$(".trash").append(olt);
						var ult = $("<ul></ul>");
						ult.addClass("relation-list");
						$(".vnav").append(ult);
					}
//					var ol = $(".trash>ol");
					var ul = $(".vnav>ul");
					for ( var i = 0; i < array.length; i++) {
						var element = array[i];
						var id = element["id"];
						var li = $("<li></li>");
						li.addClass("uRelation rnav-select");
						li.attr("rid",id);
						ul.append(li);
//						var div = $('<span class="split"></span>');
//						li.append(div);
						var name = element["name"];
						var roles = element["roles"];
						var row = $("<span></span>");
						row.attr("id", id);
						row.attr("from", "trash");
						li.append(row);
//						var relation = $("<a class='relation' href='javascript:void(0)'></a>");
						var relation = $("<span class='relation'></span>");
						relation.attr("id", id);
						relation.attr("from", "trash");
						relation.attr("title",name + "(" + roles + ")");
						relation.text(name + "(" + roles + ")");
						row.append(relation);
						var down = $("<a class='top rel_op' href='javascript:void(0)'></a>");
						if(lang=='en'){
							down.attr("title","Favourite");
				    	}else if(lang=='zh'){
				    		down.attr("title","最爱");
						}
						down.hide();
						row.append(down);
						var remove = $("<a class='restore rel_op' href='javascript:void(0)'></a>");
						if(lang=='en'){
							remove.attr("title","Restore");
				    	}else if(lang=='zh'){
				    		remove.attr("title","还原");
				    	}
						remove.hide();
						row.append(remove);
					}
					//paging information
					var container = $(".vnav");
					var navDiv = $("<div class='navfoot'></div>");
					container.append(navDiv);
					if(currentPage>1){
						var prevaLink = $('<a  class="navContent pnnav" href="javascript:void(0)"></a>');
						prevaLink.bind("click",function(){
							var mstart = (start-length)<0?0:(start-length);
							showTrash(mstart, length, newFlag, ds_type, ds_id, user, lang,currentPage-1);
						});
						if(lang=='en')
							prevaLink.text("Prev");
						else if(lang=="zh")
							prevaLink.text("上一页");
						navDiv.append(prevaLink);
					}
					/*in the view paging, 5 pages shown*/
					var startPage = (currentPage-2)>0?(currentPage-2):1;
					var endPage = (currentPage+2)>totalPage?(totalPage>0?totalPage:1):(currentPage+2);
					if(endPage>startPage){
					for(var j=startPage;j<=endPage;j++){
						if(j!=currentPage){
							var alink =$('<a class="navContent" href="javascript:void(0)"></a>');
							alink.text(j);
							alink.click(function(){
								var pointer = parseInt($(this).text());
								showTrash((pointer-1)*length, length, newFlag, ds_type, ds_id, user, lang,pointer);
							});
							navDiv.append(alink);
						}else{
							var span = $("<span class='navContent current' ></span>");
							span.text(j);
							navDiv.append(span);
						}
					}
					}
					if(currentPage<totalPage){
						var nextaLink = $('<a  class="navContent pnnav" href="javascript:void(0)"></a>');
						nextaLink.bind("click",function(){
							showTrash(start+length, length, newFlag, ds_type, ds_id, user, lang,currentPage+1);
						});
						if(lang=='en')
							nextaLink.text("Next");
						else if(lang=="zh")
							nextaLink.text("下一页");
						navDiv.append(nextaLink);
					}		
					
					//========
					
					
					if(array.length > 0){
						useRelation($(".relation-list li:first"),array[0]["id"], ds_type, ds_id,idStr, true);
					}
					$(".vnav").attr("num",array.length);
//					if (array.length > 0) {
//						var div = $("<div class='more-container'></div>");
//						var moreDiv = $("<div class='moreDiv'></div>");
//						var showText;
//						if(lang == 'en'){
//							showText = "Load More";
//						}else if(lang == 'zh'){
//							showText = "更多";
//						}
//						var a = $('<a href="javascript:void(0)"><span class="tip">'+showText+'</span><span class="arrowDown"></span></a>');
//						a.bind("click", function() {
//							showTrash(start + length, length, false, ds_type,
//									ds_id, user);
//						});
//						moreDiv.append(a);
//						div.append(moreDiv);
//						$(".trash").append(div);
//					}
				}
			});
	if($("#new_button").length>0){
		$("#new_button").hide();
	}
	return num;
}

function cancelFavorite(line, ds_type, ds_id, user) {
	var data = {};
	var id = line.attr("id");
	data["id"] = id+"";
	data["ds_type"] = ds_type;
	data["ds_id"] = ds_id+"";
	data["user"] = user;
	data = JSON.stringify(data);
	data = ("data=" + encodeURIComponent(data));
	$.ajax({
		url : "/sview/relationCancelFavorite",
		async : true,
		data : data,
		dataType : "json",
		type : "post",
		success : function(result) {
			line.parent().remove();
		}
	});
}

function cancelTrash(line, ds_type, ds_id, user) {
	var data = {};
	var id = line.attr("id");
	data["id"] = id+"";
	data["ds_type"] = ds_type;
	data["ds_id"] = ds_id+"";
	data["user"] = user;
	data = JSON.stringify(data);
	data = ("data=" + encodeURIComponent(data));
	$.ajax({
		url : "/sview/relationCancelTrash",
		async : true,
		data : data,
		dataType : "json",
		type : "post",
		success : function(result) {
			line.parent().remove();
		}
	});
}

function addTrash(line, ds_type, ds_id, user) {
	var data = {};
	var id = line.attr("id");
	var from = line.attr("from");
	data["id"] = id+"";
	data["ds_type"] = ds_type;
	data["ds_id"] = ds_id+"";
	data["user"] = user;
	data["from"] = from;
	data = JSON.stringify(data);
	data = ("data=" + encodeURIComponent(data));
	var uri = (from == "favorite") ? "relationAddTrashFromFavorite"
			: "relationAddTrashFromOrdinary";
	$.ajax({
		url : "/sview/"+uri,
		async : true,
		data : data,
		dataType : "json",
		type : "post",
		success : function(result) {
			line.parent().remove();
		}
	});
}

function addFavorite(line, ds_type, ds_id, user, from) {
	var data = {};
	var id = line.attr("id");
	var from = line.attr("from");
	data["id"] = id+"";
	data["ds_type"] = ds_type;
	data["ds_id"] = ds_id+"";
	data["user"] = user;
	data = JSON.stringify(data);
	data = ("data=" + encodeURIComponent(data));
	var uri = (from == "trash") ? "relationAddFavoriteFromTrash"
			: "relationAddFavoriteFromOrdinary";
	$.ajax({
		url : "/sview/"+uri,
		async : true,
		data : data,
		dataType : "json",
		type : "post",
		success : function(result) {
			line.parent().remove();
		}
	});
}
